# uncompyle6 version 2.11.5
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.9 (default, Sep 17 2016, 20:26:04) 
# [GCC 4.9.2]
# Embedded file name: D:\project\inleader_frist_client\print_client\core\UI_thread.py
# Compiled at: 2014-01-10 07:56:48
__author__ = 'prj'
import commands
import threading
import os
import zipfile
import time
from utils.http import UrllibHttpUtils
from setting import setting, Vfs
from download import AxelPython
IS_REFRESH = False

class UIThread(threading.Thread):

    def __init__(self, uiMain):
        threading.Thread.__init__(self)
        self.isQuit = threading.Event()
        self.isQuit.clear()

    def stop(self):
        self.isQuit.set()
        self.join()

    def _execute(self):
        global IS_REFRESH
        if setting.config is not None and setting.config.has_option('api', 'html_file'):
            response = UrllibHttpUtils.callRpc('get_zip_url', {'macaddress': setting.getMac()})
            if response is None:
                return
            html_file = response['clientZip']
            if html_file is not None:
                down_zip_content = os.path.join(Vfs.getTempDir(), 'temp.zip')
                self.ap = AxelPython(html_file, down_zip_content, 8, proxies={})
                self.ap.paxel(html_file, down_zip_content, blocks=6, proxies={})
                print ('download init html success.\n load system...')
                try:
                    try:
                        with zipfile.ZipFile(down_zip_content) as zip:
                            zip.extractall(Vfs.getHtmlDir())
                            UrllibHttpUtils.callRpc('update_zip_status', {'macaddress': setting.getMac()})
                            IS_REFRESH = True
                    except:
                        print ('html error')
                        IS_REFRESH = False

                finally:
                    Vfs.deleteTempFile(down_zip_content)

            else:
                print ('download init html error')
                return
        return

    def run(self):
        while not self.isQuit.isSet():
            try:
                self._execute()
                time.sleep(60)
            except:
                setting.logger.exception('error')